/*
 *   This file is part of SRS project.
 *
 *   SRS is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   SRS is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with SRS. If not, see <http://www.gnu.org/licenses/>.
 *
 *   Created by: Yifang Sun, Jianbin Qin
 *   Last modified by: Yifang Sun, Jianbin Qin
 */

#ifndef SRSINMEMORY_H_
#define SRSINMEMORY_H_

#include <stdio.h>
#include <vector>
#include <algorithm>

#include "ParamFile.h"
#include "RandGen.h"
#include "SRSCoverTree.h"
#include "RawData.h"

template<typename T>
struct res_pair_raw {
  int id;
  T dist;
  bool operator>(const res_pair_raw<T> &) const;
  bool operator>=(const res_pair_raw<T> &) const;
  bool operator==(const res_pair_raw<T> &) const;
  bool operator<=(const res_pair_raw<T> &) const;
  bool operator<(const res_pair_raw<T> &) const;
};


template<typename T>
class SRS_In_Memory {
 private:
  long long n;
  int d;
  int m;
  float * proj;
  Raw_data<T> * raw_data;
  char * index_path;
  char * data_type;
  SRS_Cover_Tree * index;

  void get_proj(int n, int d, T * source, float * proj, float * dest);

 public:
  SRS_In_Memory(char * index_path);
  virtual ~SRS_In_Memory();

  void build_index(long long n, int d, int m, char * ds_path);
  void restore_index();
  template<typename X>
  void knn_search(T * query, int k, int t, double thres,
                  std::vector<res_pair_raw<X> > & heap);
  int get_m() {
    return this->m;
  }
  char * get_type() {
    return this->data_type;
  }
};

#endif /* SRSINMEMORY_H_ */
